<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of capitalAction
 *
 * @author wcj
 */
class commission_summaryAction extends backendAction {

    public function _initialize() {
        parent::_initialize();
        $this->_mod = D('price_log_order');
        $this->tgroup = [100 => '普通用户', 1 => '总经销商', 2 => '经销商', 3 => '代理商', 4 => '经纪人'];
    }

    public function index() {
        $map = $this->_search();
        $mod = $this->_mod;
        if (!empty($mod)) {
            $result = $this->_list($mod, $map);
            $this->assign('page', $result['page']);

            if ($this->_request('export') != '') {
                $list = $mod->where($map)->select();
            } else {
                $list = $result['list'];
            }

            foreach ($list as $key => $val) {

                $list[$key]['tgroup_name'] = get_tgroup($val['tgroup']);
                $list[$key]['tgroup_1_name'] = get_username($val['tgroup_1']);
                $list[$key]['tgroup_2_name'] = get_username($val['tgroup_2']);
                $list[$key]['tgroup_3_name'] = get_username($val['tgroup_3']);
                $list[$key]['tgroup_4_name'] = get_username($val['tgroup_4']);
                $list[$key]['tgroup_5_name'] = get_username($val['tgroup_5']);
                
                $list[$key]['tele'] = get_tele_by_username($val['uname']);

                $list[$key]['update_status'] = get_update_status($val['id']);
                $list[$key]['order_status'] = get_order_status($val['status']);

                $list[$key]['tgroup_5_id'] = get_tgroup_5($val['uid']);
                $list[$key]['order_username'] = get_username($val['uid']);
            }

            if ($this->_request('export') != '') {
                Vendor('excelClass.excelclass');
                $excel = new excelClass();
                $excel->echocommissionSummary('全民经纪人返佣汇总表' . date('YmdHis') . '.xls', $list);
                exit;
            }
//            return print_r($list);
            $this->assign('list', $list);


            $arr = $mod->where($map)->select();
            foreach ($arr as $key => $val) {

                $sum += $val[prices];
                $sum1 += $val[price];
            }

            $list1 = D('team')->field('title')->select();
            $arr = i_array_column_ext($list1, 'title');
            $this->assign('topkey', $arr);
            $this->assign('sum', $sum);
              $this->assign('sum1', $sum1);
            $this->assign('lottery', [0 => '未升级', 1 => '升级中', 2 => '升级成功', 9 => '升级失败']);
            $this->assign('status', [100 => '未付款', 1 => '已付款', 2 => '已发货', 3 => '已确认', 4 => '已评价', 5 => '已成功', 7 => '待处理退款', 8 => '已退款', 9 => '已取消']);
            $this->display();
        }
    }

    protected function _search() {
        $map = array();
        ($uid = $this->_request('uid', 'trim')) && $map['uid'] = $uid;
         ($tgroup_5 = $this->_request('tgroup_5', 'trim')) && $map['id'] = $tgroup_5;
        ($tele = $this->_request('tele', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($tele, 'tele'));
        ($realname = $this->_request('realname', 'trim')) && $map['uid'] = array('IN', $this->_get_uids($realname, 'realname'));
        // ($remark = $this->_request('remark', 'trim')) && $map['uid'] = array('IN',$this->_get_uids($remark,'remark'));
        //7.15

         if ($_GET['username']) {
        $realname= trim($_GET['username']);
        $map['username'] = array('like', '%' . $_GET['username'] . '%','AND');
            
        }
        if ($_GET['tgroup']) {
            $tgroup = $_GET['tgroup'];
            $tgroup = ($_GET['tgroup'] != 100) ? $_GET['tgroup'] : 0;
            $map['tgroup'] = $tgroup;
        }


        if ($_GET['topkey']) {
            $topkey = $_GET['topkey'];
            $map['topkey'] = $topkey;
        }

        if ($_GET['stime']) {
            $time_start = $_GET['stime'] . ' 00:00:00';
            $time_end = $_GET['etime'] . ' 23:59:59';
            $map['add_time'] = array(array('egt', $time_start), array('elt', $time_end));
        }
        //7.15
        $type = intval($_GET['type']);
        $type = $type ? array('eq', $type) : array('eq', 1);
        $map['type'] = $type;

        $this->assign('search', array(
            'keyword' => $keyword,
            'tele' => $tele,
            'username' => $_GET['username'],
            'tgroup_5' => $_GET['tgroup_5'],
            'topkey' => $_GET['topkey'],
            'status' => $status,
            'type' => $type[1],
            'lottery' => $_GET['lottery'],
            'status' => $_GET['status'],
            'stime' => $_GET['stime'],
            'etime' => $_GET['etime'],
        ));


        return $map;
    }

    public function _get_uids($keyword, $field) {
        $where[$field] = array('like', '%' . $keyword . '%');
        $uidarr = D('user')->where($where)->field('id')->select();
        $_idarr = array();
        foreach ($uidarr as $v) {
            $_idarr[] = $v['id'];
        }
        return implode(',', $_idarr);
    }

}
